<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>公司信息维护</title>
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link rel="stylesheet" type="text/css" href="${prefix}/stylesheets/theme.css">
<link rel="stylesheet" type="text/css" href="${prefix}/stylesheets/reveal.css">
<link rel="stylesheet" type="text/css" href="${prefix}/lib/font-awesome/css/font-awesome.css">
<link rel="stylesheet" type="text/css" href="${prefix}/lib/bootstrap/css/bootstrap.css">

<script src="${prefix}/lib/jquery-1.8.1.min.js" type="text/javascript"></script>
<script src="${prefix}/lib/ajaxfileupload.js" type="text/javascript"></script>
<script src="${prefix}/lib/jquery-form.js" type="text/javascript"></script>
<script src="${prefix}/lib/jquery.reveal.js" type="text/javascript"></script>
<style type="text/css">
#line-chart {
	height:300px;
	width:800px;
	margin: 0px auto;
	margin-top: 1em;
}

.brand { font-family: georgia, serif; }
.brand .first {
	color: #ccc;
	font-style: italic;
}

.brand .second {
	color: #fff;
	font-weight: bold;
}
</style>

<script type="text/javascript">
$(document).ready(function () {
	//当公司选择发生变化时
	$("#savebutton").click(function() {
		var goodForm = true;
		var error = "";
		
		//=====进行表单提交前的验证
		if($("#companyName").val() == "") {
			goodForm = false;
			error += "请填写公司名称（最多50个字）\r";
		}
		
		if(!goodForm) {
			alert(error);
			return;
		}
		
		//=============提交表单
		$("#companyForm").submit();
	});
	
	//为指定公司添加商铺的事件
	$("#savestorebutton").click(function() {
		var goodForm = true;
		var error = "";
		
		//首先进行填写资料的判断
		if($("#repeatAdmin").val() == "true") {
			goodForm = false;
			error += "该管理员用户名已被使用，请更换一个！\r";
		}
		var storeName = $.trim($("#storeName").val());
		if(storeName == "") {
			goodForm = false;
			error += "店面名称必须填写！<br>";
		}
		var storephone = $.trim($("#storephone").val());
		if(storephone == "") {
			goodForm = false;
			error += "联系电话必须填写！<br>";
		}
		var storeadminname = $.trim($("#storeadminname").val());
		if(storeadminname == "") {
			goodForm = false;
			error += "店面管理员必须填写！<br>";
		}
		var storeadminpassword = $.trim($("#storeadminpassword").val());
		if(storeadminpassword == "") {
			goodForm = false;
			error += "店面管理密码必须填写！<br>";
		}
		if(!goodForm) {
			alert(error);
			return;
		}
		
		//==============提交到后台进行添加
		var storeadd = $("#storeadd").val();
		var companyid = $("#uid").val();
		var data = encodeURI("storeName=" + storeName + "&storephone=" + storephone + "&storeadminname=" + storeadminname + "&storeadminpassword=" + storeadminpassword + "&companyid=" + companyid + "&storeadd=" + storeadd);
		$.ajax({
			type: "post",
			async: true,
			url: "${prefix}/storefront/appendStorefront",
			data: data,
			success: function(data) {
				var jsonData = eval('(' + data + ')');
				var responsecode = jsonData.responsecode
				if(responsecode != "E200") {
					alert("添加店面错误，请刷新后再试：" + jsonData.errorinfo);
					return;
				}
				
				var newStore = jsonData.data;
				
				//======一旦更新成功，则首先清空表单页面，然后将这个信息刷新到“门店信息”列表 。最后弹一个提示信息
				//清空表单页面
				$("#repeatAdmin").val(false);
				$("#storeName").val("");
				$("#storephone").val("");
				$("#storeadminname").val("");
				$("#storeadminpassword").val("");
				$("#storeadd").val("");
				
				//刷新到“门店信息”列表
				var tdhtml = "<tr>";
				tdhtml += "<td>" + storeName + "</td>";
				tdhtml += "<td>" + storephone + "</td>";
				tdhtml += "<td>" + newStore.createTime + "</td>";
				tdhtml += "<td> 正常 </td>";
				tdhtml += "<td>";
				tdhtml += "<a href=\"javascript:void(0);\" onclick=perStoreModify('" + newStore.uid + "') title=\"编辑\" style=\"margin-right: 10px\"  data-reveal-id=\"myStoreDiv\"><i class=\"icon-pencil\"></i></a>";
				tdhtml += "<a href=\"javascript:void(0);\" onclick=doStoreStatus('" + newStore.uid + "') title=\"作废\" style=\"margin-right: 10px\"><img src=\"${prefix}/images/delete.png\"></a>";
				tdhtml += "</td>";
				tdhtml += "<tr>";
				
				$("#storetbodydiv").prepend(tdhtml);
				
				//提示信息
				alert("门店信息添加成功 ，您可以继续添加，也可以关闭该对话框！");
			}
		});
	});
	
	//当管理员用户名发生变化时，判断这个管理员用户名是否可用
	$("#storeadminname").focusout(function() {
		var storeadminname = $.trim($("#storeadminname").val());
		if(storeadminname == "") {
			return;
		}
		
		//ajax进行判断
		$.ajax({
			type: "post",
			async: true,
			url: "${prefix}/adminuser/queryAdminUser?userName=" + storeadminname,
			success: function(data) {
				var jsonData = eval('(' + data + ')');
				var returndata = jsonData.data;
				if(returndata != null) {
					$("#repeatAdmin").val(true);
					$("#storeadminnameError").html("该管理员用户名已被使用，请更换一个！");
				} else {
					$("#repeatAdmin").val(false);
					$("#storeadminnameError").html("");
				}
			}
		});
		
	});
	
	//======如果存在公司的uid信息，那么还要查询这个公司下的店面信息 
	if($("#uid").val() != '') {
		queryStorebyCompany();
	}
});

//按照指定的公司id，查询当前公司下的店面信息，并显示在列表中
function queryStorebyCompany() {
	$.ajax({
		type: "post",
		async: true,
		url: "${prefix}/storefront/queryStorefrontList?companyid=${company.uid}",
		success: function(data) {
			var jsonData = eval('(' + data + ')');
			var listDatas = jsonData.data;
			
			for(var index = 0 ; listDatas != null && index < listDatas.length ; index++) {
				var tdItem = listDatas[index];
				
				var tdhtml = "<tr>";
				tdhtml += "<td>" + tdItem.storeName + "</td>";
				tdhtml += "<td>" + tdItem.storePhone + "</td>";
				tdhtml += "<td>" + tdItem.createTime + "</td>";
				if(tdItem.status == 1) {
					tdhtml += "<td> 正常 </td>";
				} else {
					tdhtml += "<td style=\"color:#FF0000\"> 作废 </td>";
				}
				
				tdhtml += "<td>";
				tdhtml += "<a href=\"javascript:void(0);\" onclick=perStoreModify('" + tdItem.uid + "') title=\"编辑\" style=\"margin-right: 10px\"  data-reveal-id=\"myStoreModifyDiv\"><i class=\"icon-pencil\"></i></a>";
				if(tdItem.status == 1) {
					tdhtml += "<a href=\"javascript:void(0);\" onclick=doStoreStatus('" + tdItem.uid + "') title=\"作废\" style=\"margin-right: 10px\"><img src=\"${prefix}/images/delete.png\"></a>";
				} else {
					tdhtml += "<a href=\"javascript:void(0);\" onclick=doStoreStatus('" + tdItem.uid + "') title=\"启用 \" style=\"margin-right: 10px\"><img src=\"${prefix}/images/yes2.png\"></a>";
				}
				
				tdhtml += "</td>";
				tdhtml += "<tr>";
				
				$("#storetbodydiv").append(tdhtml);
			}
		}
	});
}

//该方法展现 将要进行修改的店面信息
function perStoreModify(storeuid) {
	$.ajax({
		type: "post",
		async: true,
		url: "${prefix}/storefront/queryStorefront?storeid=" + storeuid,
		success: function(data) {
			var jsonData = eval('(' + data + ')');
			var store = jsonData.data;
			
			//然后将这个店面信息赋值到myStoreModifyDiv层
			$("#storeName_modify").val(store.storeName);
			$("#storeadd_modify").val(store.storeAddress);
			$("#storephone_modify").val(store.storePhone);
			$("#storeuid_modify").val(store.uid);
		}
	});
}

//该方法进行当前选中店面的基本信息修改
function doStoreModify() {
	//首先进行信息的正确性验证
	var goodForm = true;
	var error = "";
	
	var storeName = $.trim($("#storeName_modify").val());
	if(storeName == "") {
		goodForm = false;
		error += "店面名称必须填写！<br>";
	}
	var storephone = $.trim($("#storephone_modify").val());
	if(storephone == "") {
		goodForm = false;
		error += "联系电话必须填写！<br>";
	}
	if(!goodForm) {
		alert(error);
		return;
	}
	
	var storeid = $.trim($("#storeuid_modify").val());
	var storeadd = $.trim($("#storeadd_modify").val());
	//==========提交到后台进行处理
	var data = encodeURI("storeid=" + storeid + "&storeName=" + storeName + "&storephone=" + storephone + "&storeadd=" + storeadd);
	$.ajax({
		type: "post",
		async: true,
		url: "${prefix}/storefront/updateStorefront",
		data: data,
		success: function(data) {
			var jsonData = eval('(' + data + ')');
			var store = jsonData.data;
			
			//将最新的信息更新到列表上面（为了简单，这里重新查询列表）
			$("#storetbodydiv").html("");
			queryStorebyCompany();
			
			//提示更新成功
			alert("门店信息修改成功 ，您可以继续修改，也可以关闭该对话框！");
		}
	});
}

//作废或者重新启用指定的店面（这个操作在店面没有付服务费 的情况下，关闭店面服务）
function doStoreStatus(storeuid) {
	$.ajax({
		type: "post",
		async: true,
		url: "${prefix}/storefront/updateStorefrontStatus?storeid=" + storeuid,
		success: function(data) {
			var jsonData = eval('(' + data + ')');
			var store = jsonData.data;
			
			//将最新的信息更新到列表上面（为了简单，这里重新查询列表）
			$("#storetbodydiv").html("");
			queryStorebyCompany();
		}
	});
}
</script>
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
	<script src="${prefix}/javascripts/html5.js"></script>
<![endif]-->
<!-- Le fav and touch icons -->
<link rel="shortcut icon" href="${prefix}/lib/font-awesome/docs/assets/ico/favicon.ico">
<link rel="apple-touch-icon-precomposed" href="${prefix}/lib/font-awesome/docs/assets/ico/apple-touch-icon-144-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="${prefix}/lib/font-awesome/docs//assets/ico/apple-touch-icon-114-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="${prefix}/lib/font-awesome/docs//assets/ico/apple-touch-icon-72-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="${prefix}/lib/font-awesome/docs//assets/ico/apple-touch-icon-57-precomposed.png">
</head>

<!--[if lt IE 9]>
<style>
.reveal-modal-bg{filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#cc000000,endColorstr=#cc000000);}
</style>
<![endif]-->

<!--[if lt IE 7 ]> <body class="ie ie6"> <![endif]-->
<!--[if IE 7 ]> <body class="ie ie7"> <![endif]-->
<!--[if IE 8 ]> <body class="ie ie8"> <![endif]-->
<!--[if IE 9 ]> <body class="ie ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> 
<body>
<!--<![endif]-->
<div class="navbar">
	<%@ include file="/commonjsp/userbar.jsp"%>
</div>

<div class="container-fluid">
	<div class="row-fluid">
		<%@ include file="/commonjsp/mainmenu.jsp"%>
		
		<!-- app 资源详情 -->
		<div class="span9">
			<h1 class="page-title">公司信息</h1>
			
			<!-- 操作按钮 -->
			<div class="btn-toolbar">
				<a id="savebutton"  href="javascript:void(0);" class="btn btn-primary"><i class="icon-save"></i> Save</a>
				<div class="btn-group"></div>
				<a href="${prefix}/company/preCompanyList" class="btn">返回列表</a>
				<c:if test="${company.uid != null && company.uid != ''}">
				<div class="btn-group"></div>
				<a href="javascript:void(0);" class="btn" data-reveal-id="myStoreDiv">创建店面</a>
				</c:if>
			</div>
			<div class="well">
				<ul class="nav nav-tabs">
					<li class="active"><a href="#home" data-toggle="tab">公司信息</a></li>
					<c:if test="${company.uid != null && company.uid != ''}">
					<li><a href="#storeversiondiv" data-toggle="tab">店面信息</a></li>
					</c:if>
				</ul>
				
				<div id="myTabContent" class="tab-content">
					<!-- 公司基本信息 -->
					<div class="tab-pane active in" id="home" >
					<form action="${prefix}/company/saveCompany" method="post" name="companyForm" id="companyForm" enctype="multipart/form-data">
					<input type="hidden" name="uid" id="uid" value="${company.uid }"/>
					<input type="hidden" name="createTime" id="createTime" value="${company.createTime }"/>
					<input type="hidden" name="status" id="status" value="${company.status }"/>
					<input type="hidden" name="companyURL" id="companyURL" value="${company.companyURL }"/>
						<label>公司名称</label>
						<input type="text" id="companyName" value="${company.companyName }" name="companyName" class="input-xlarge" maxlength="50">
						<label>所属行业</label>
						<select name="industryType" id="industryType">
							<option value="101" <c:if test="${company.industryType == '101' }">selected</c:if>> KTV </option>
							<option value="102" <c:if test="${company.industryType == '102' }">selected</c:if>> 酒店住宿 </option>
							<option value="103" <c:if test="${company.industryType == '103' }">selected</c:if>> 夜场 </option>
							<option value="104" <c:if test="${company.industryType == '104' }">selected</c:if>> 其他 </option>
						</select>
						<c:if test="${company.uid != null && company.uid != ''}">
						<label>创建时间：</label>
						2015-09-09 19:30:01
						</c:if>
						<label>公司Log图标（最大100kb）：<input type="file" name="companyURLFile" id="companyURLFile" value="选择文件"/></label>
						<c:if test="${company.uid != null && company.uid != ''}">
							<c:choose>
								<c:when test="${company.companyURL == null || company.companyURL == ''}">
									<img src="${prefix}/images/defaultcompany.png" style="width: 32px"/>
								</c:when>
								<c:otherwise>
									<img src="${company.companyURL}"/>
								</c:otherwise>
							</c:choose>
						</c:if>
					</form>
					</div>

					<!-- 店面信息 -->
					<div class="tab-pane fade" id="storeversiondiv">
						<font color="#999999">公司：${company.companyName } 下的店铺信息</font><br/>
						<table class="table">
							<thead>
								<tr>
									<th>店面名称</th>
									<th>联系电话</th>
									<th>创建时间</th>
									<th>店面状态</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody id="storetbodydiv">
								
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
	</div>
    <!-- Le javascript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="${prefix}/lib/bootstrap/js/bootstrap.js"></script>
    
    <!-- 添加店面 -->
	<div id="myStoreDiv" class="reveal-modal" style="width: 480px;height: 420px; overflow-x: hidden; overflow-y: hidden">
		<input type="hidden" name="repeatAdmin" id="repeatAdmin" value="false"/>
		<label>创建新的店面</label>
		<label>店面名称</label>
		<input type="text" id="storeName" name="storeName" value="" class="input-xlarge" maxlength="50">
		<font style="color:#FF0000">*</font>
		<label>店面地址</label>
		<input type="text" id="storeadd" name="storeadd" value="" class="input-xlarge" maxlength="100">
		<label>联系电话</label>
		<input type="text" id="storephone" name="storephone" value="" class="input-xlarge" maxlength="50">
		<font style="color:#FF0000">*</font>
		<label>管理员信息-用户名</label>
		<input type="text" id="storeadminname" name="storeadminname" value="" class="input-xlarge" maxlength="50">
		<font style="color:#FF0000">*</font>
		<font style="color:#FF0000" id="storeadminnameError"></font>
		<label>管理员信息-用户密码</label>
		<input type="password" id="storeadminpassword" name="storeadminpassword" value="" class="input-xlarge" maxlength="50">
		<font style="color:#FF0000">*</font>
		<a class="close-reveal-modal">×</a>
		<label></label>
		<button id="savestorebutton" class="btn btn-primary"><i class="icon-save"></i> 创 建 </button>
	</div>
	
	<!-- 修改店面 -->
	<div id="myStoreModifyDiv" class="reveal-modal" style="width: 480px;height: 340px; overflow-x: hidden; overflow-y: hidden">
		<input type="hidden" id="storeuid_modify" value="" class="input-xlarge" maxlength="50">
		<label>创建新的店面</label>
		<label>店面名称</label>
		<input type="text" id="storeName_modify" value="" class="input-xlarge" maxlength="50">
		<font style="color:#FF0000">*</font>
		<label>店面地址</label>
		<input type="text" id="storeadd_modify" value="" class="input-xlarge" maxlength="100">
		<label>联系电话</label>
		<input type="text" id="storephone_modify" value="" class="input-xlarge" maxlength="50">
		<font style="color:#FF0000">*</font>
		<a class="close-reveal-modal">×</a>
		<label>注意：如果需要对“店面管理员”进行维护，请到“管理员维护功能”中进行</label>
		<label></label>
		<button id="updatestorebutton" class="btn btn-primary" onclick="doStoreModify()"><i class="icon-save"></i> 修 改 </button>
	</div>
</div>
</body>
</html>